/* eslint-disable @typescript-eslint/no-unused-vars */
import { Body, Controller, Logger, Get, Post, Req } from '@nestjs/common';

import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';

import { ApiFail } from '@common/utils';
import { SocketService } from './socket/socket.service';
import { MyWebSocketGateway } from './web-socket.gateway';
import { RedisService } from '@common/reids/redis.service';
import { WebSocketService } from './web-socket.service';
import { RoomDto } from './dto/room.dto';

@ApiTags('MINI-同步消息，http转socket')
@Controller()
export class WebSocketController {
  logger: Logger;
  constructor(
    public readonly socketService: SocketService,
    public readonly webSocketService: WebSocketService,
    public readonly redisService: RedisService,
    public readonly gateway: MyWebSocketGateway,
  ) {
    this.logger = new Logger('WebSocket');
  }

  @Post('sync')
  async syncAction(@Body() body: any) {
    const { action, data } = body;
    const blackApi = [
      'joinGroup',
      'exitGroup',
      'messageGroup',
      'message',
      'close',
      // 'friendDel',
      // 'friendAgree',
      // 'invitedFriend',
      'joinInvitedGroup',
      'joinAgreeGroup',
      'destoryRoom',
      'reconnectGroup',
      'systemMessage',
    ];
    if (!blackApi.includes(action)) throw new ApiFail(500, '非法操作');
    // 这里需要用到非socket 进程通讯
    // global.$socketChat.io &&
    //   global.$socketChat.io.emit(action, JSON.parse(data));
    this.gateway[action].apply(this, [null, JSON.parse(data)]);
  }
  @Post('isExist')
  @ApiBearerAuth()
  async isExistAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.isExist(body, req);
  }
  @Post('createRoom')
  @ApiBearerAuth()
  async createRoomAction(@Body() body: any, @Req() req: any) {
    return this.webSocketService.createRoomAction(body, req);
  }
  @Post('userJoinByExistRoom')
  @ApiBearerAuth()
  async userJoinByExistRoomAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.userJoinByExistRoomAction(body);
  }
  @Post('onReady')
  @ApiBearerAuth()
  async onReady(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.ready(body);
  }
  @Post('begin')
  @ApiBearerAuth()
  async begin(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.begin(body,req);
  }
  @Post('beginBefore')
  @ApiBearerAuth()
  async beginBefore(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.beginBefore(body,req);
  }
  @Post('gameOver')
  @ApiBearerAuth()
  async gameOver(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.gameOver(body);
  }
  @Post('joinRoom')
  @ApiBearerAuth()
  async joinRoomAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.joinRoomAction(body);
  }
  @Post('exitRoom')
  @ApiBearerAuth()
  async exitRoomAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.exitRoomAction(body);
  }
  @Post('tirenRoom')
  @ApiBearerAuth()
  async tirenRoomAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.tirenRoomAction(body);
  }
 
  @Post('getRoomInfo')
  @ApiBearerAuth()
  async getRoomInfoAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.getRoomInfoAction(body);
  }
  @Post('getRoomUsers')
  @ApiBearerAuth()
  async getRoomUsersAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.getRoomUsersAction(body);
  }
  @Post('getUserInfo')
  @ApiBearerAuth()
  async getUserInfoAction(@Body() body: RoomDto, @Req() req: any) {
    return this.webSocketService.getUserInfoAction(body);
  }
  @Post('allUsersByRoom')
  @ApiBearerAuth()
  async allUsersByRoomAction(@Body() body: any) {}
  @Post('sortUserByRoom')
  @ApiBearerAuth()
  async sortUserByRoomAction(@Body() body: any, @Req() req: any) {
    return this.webSocketService.sortUserByRoomAction(body);
  }
  @Post('roleRoom')
  @ApiBearerAuth()
  async roleRoomAction(@Body() body: any, @Req() req: any) {
    return this.webSocketService.roleRoomAction(body);
  }
}
